"""
Define the REST verbs relative to the portfolios
"""

from flasgger import swag_from
from flask.json import jsonify
from flask_restful import Resource
from flask_restful.reqparse import Argument

from repositories import PortfolioRepository
from util import parse_params


class PortfolioResource(Resource):
    """ Verbs relative to the portfolios """

    @staticmethod
    @swag_from("../swagger/portfolio/GET.yml")
    def get(code):
        """ Return an portfolio key information based on code number """
        portfolio = PortfolioRepository.get(code=code)
        return jsonify({"portfolio": portfolio.json})

    @staticmethod
    @parse_params(
        Argument("age", location="json", required=True, help="The age of the portfolio.")
    )
    @swag_from("../swagger/portfolio/POST.yml")
    def post(code, name, price):
        """ Create an portfolio based on the sent information """
        portfolio = PortfolioRepository.create(
            code=code, name=name, price=price
        )
        return jsonify({"portfolio": portfolio.json})

    @staticmethod
    @parse_params(
        Argument("price", location="json", required=True, help="The price of the portfolio.")
    )
    @swag_from("../swagger/portfolio/PUT.yml")
    def put(code, name, price):
        """ Update an portfolio based on the sent information """
        repository = PortfolioRepository()
        portfolio = repository.update(code=code, name=name, price=price)
        return jsonify({"portfolio": portfolio.json})
